///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009 Summit Contributors.  
// See: http://code.google.com/p/tyemill/wiki/SummitContributors
//
// The name and logo of Summit are trademarks of TYEMILL, LLC - All Rights 
// Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without 
// limitation the rights to use, copy, modify, merge, publish, distribute, 
// sublicense, and/or sell copies of the Software, and to permit persons to 
// whom the Software is furnished to do so, subject to the following 
// conditions:
//
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
///////////////////////////////////////////////////////////////////////////////

package summit.basecamp.wrappers
{
import summit.basecamp.constants.BasecampResource;
import summit.basecamp.helpers.TimeEntryHelper;
import summit.data.AccountToken;
import summit.net.BasecampLoader;

/**
 * Wrapper for Basecamp time-entry operations.
 */
public class TimeEntryWrapper extends AbstractWrapper implements
IStandardWrapper, IListableWrapper
{
	/** Constructor.
	 * @param token The <code>AccountToken</code> that is to be used in 
	 * identifying the Basecamp account to which make API calls are made.
	 * @param loader The <code>BasecampLoader</code> that is to be used 
	 * with all requested operations.
	 */
    public function TimeEntryWrapper(token:AccountToken = null, 
	loader:BasecampLoader = null)
	{
		super(BasecampResource.TIMEENTRY, token, loader);
	}
	
	/**
	 * Creates a new time-entry.
	 * 
	 * <p>Typically, the XML provided is based on the template returned from
	 * a call to <code>prepare</code>.
	 * 
	 * <p><code>project-id</code>, which represents the project that is 
	 * associated with time-entry, is required. <code>person-id</code> 
	 * and <code>date</code>are also required.</p>
	 * 
	 * <p>In order for a time-entry to be created for a todo-item, the content 
	 * must contain a reference to an existing todo-item with the 
	 *<code>todo-item-id</code>. If a <code>todo-item-id</code> is not 
	 * included, the entry is associated with a project referenced in the 
	 * <code>project-id</code> element of the content.</p>
     *
	 * @param content Represents the time-entry to be created.
	 */
	public function create(content:XML):void
	{
		loader.load(TimeEntryHelper.create(content));
	}
	
	/**
	 * Deletes an existing time-entry.
	 * 
	 * @param id The id of the time-entry to be deleted.
	 */ 
	public function destroy(id:String):void
	{
		loader.load(TimeEntryHelper.destroy(id)); 
	}
	
	/**
	 * Gets an existing time-entry from the Basecamp store.
	 * 
	 * <p>The XML returned is pre-filled with the existing values of
	 * the identified time-entry, and is ready to be resubmitted via the 
	 * <code>update</code> function.</p>
	 * 
	 * @param id The id of the time-entry to be retrieved.
	 * @param projectId The id of the project associated with the time-entry.
	 */ 
	public function edit(id:String, projectId:String):void
	{
		loader.load(TimeEntryHelper.edit(id));
	}
	
	/**
	 * Gets a list of time-entries for the specified project.
	 * 
	 * <p>For lists of entries associated with projects, the first 50 are 
	 * returned in descending order by date. </p>
	 * 
	 * @param projectId The id of a project.
	 */
	public function list(projectId:String):void
	{
		loader.load(TimeEntryHelper.list(projectId));
	}
	
	/**
	 * Gets a list of time-entries associated with the specificed todo-item.
	 * 
	 * <p>All time-entries are retrieved in decending order by date.</p>
	 * 
	 * @param todoItemId The id of the todo-item from which time-entries are 
	 * retrieved. 
	 */
	public function listForTodoItem(todoItemId:String):void
	{
		loader.load(TimeEntryHelper.list(todoItemId, true));
	}
	
	/**
	 * Retrieves a template for creating a new time-entry.
	 * 
	 * <p>The template retrieved is used with <code>create</code> to 
	 * submit a new time-entry.</p>
	 * 
	 * @param projectId The id of the project that is to be associated with 
	 * the time-entry when submitted via a call to <code>create</code>.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 */
	public function prepare(projectId:String):void
	{
		loader.load(TimeEntryHelper.prepare(projectId));
	}
	
	/**
	 * Gets a single time-entry.
	 * 
	 * @param id The id of the time-entry to be read.
	 * @param projectId The id of the project that is associated with the 
	 * the time-entry.
	 */
	public function read(id:String, projectId:String):void
	{
		loader.load(TimeEntryHelper.read(id));
	}
	
	/**
	 * Produces a report of entries that match a given criteria.
	 * 
	 * <p>This action accepts the following query parameters: 
	 * “from”, “to”, “subject_id”, “todo_item_id”, “filter_project_id”, 
	 * and “filter_company_id”.</p>
	 * 
	 * <p>Both “from” and “to” should be dates in “YYYYMMDD” format, 
	 * and can be used to restrict the result to a particular date 
	 * range. (No more than 6 months’ worth of entries may be 
	 * returned in a single query, though).</p>
	 * 
	 * <p>The “subject_id” parameter lets you constrain the result 
	 * to a single person’s time-entries. “todo_item_id” restricts 
	 * the result to only those entries relating to the given todo-item. 
	 * “filter_project_id” restricts the entries to those for the given 
	 * project, and “filter_company_id” restricts the entries to those 
	 * for the given company.</p>
	 * 
	 * @param query The query string to be added to the report request. The 
	 * query should include the '?' with the arguments.
	 */
	public function report(query:String):void
	{
		loader.load(TimeEntryHelper.report(query));
	}
	
	/**
	 * Updates an existing time-entry.
	 * 
	 * <p><code>content</code> submitted to <code>update</code> is typically
	 * retrieved from <code>edit</code>.</p>
	 * 
	 * @param content The updated XML content of an existing time-entry.
	 */ 
	public function update(content:XML):void
	{
		loader.load(TimeEntryHelper.update(content));
	}
}
}
//////////////////////////////////////////////////////// gmarius@tyemill.com //